//
//  EssenceRootViewController.m
//  百思不得姐首页
//
//  Created by uproject on 16/8/21.
//  Copyright © 2016年 heima. All rights reserved.
//

#import "EssenceRootViewController.h"
#import "BSXPFViewController.h"
#import "HyScrollViewModel.h"
#import "baseBtn.h"
#import "BSHyCollectionViewCell.h"

@interface EssenceRootViewController () <UICollectionViewDataSource,UICollectionViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *hyScrollView;
@property (weak, nonatomic) IBOutlet UICollectionView *hyCollectionView;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *hyFlowLayout;
@property(nonatomic, strong) NSArray *topArray;
@property (nonatomic, weak) baseBtn *lastBtn;
@property(nonatomic, strong) NSMutableArray *btns;

@end

@implementation EssenceRootViewController
#pragma mark - 添加按钮到scrollView
- (void)loadDataAndAddBtns{
    CGFloat btnW = self.view.bounds.size.width/6;
    CGFloat btnH = self.hyScrollView.bounds.size.height;
    for (NSInteger i = 0; i < self.topArray.count; i++) {
        CGFloat btnX = i * btnW;
        baseBtn *btn = [[baseBtn alloc]initWithFrame:CGRectMake(btnX, 0, btnW, btnH)];
        HyScrollViewModel *model = self.topArray[i];
        [btn setTitle:model.channel_name forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        btn.tag = i;
        if (i == 0) {
            [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
            btn.scale = 1;
            self.lastBtn = btn;
        }
        [btn addTarget:self action:@selector(didClickBtn:) forControlEvents:UIControlEventTouchUpInside];
        [self.btns addObject:btn];
        [self.hyScrollView addSubview:btn];
        
    }
    self.hyScrollView.contentSize = CGSizeMake(btnW * self.topArray.count, 0);
    self.hyScrollView.panGestureRecognizer.delaysTouchesBegan = YES;
    self.hyScrollView.showsHorizontalScrollIndicator = NO;
    
}
#pragma mark - scrollview上按钮的点击事件
- (void)didClickBtn:(baseBtn *)sender{
    [self moveBtnToCenter:sender];
    self.lastBtn.scale = 0;
    [self.lastBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [self jumpToCollectionCell:sender];
    sender.scale = 1;
    [sender setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    self.lastBtn = sender;
}
#pragma mark - colletionView跳转到指定cell
- (void)jumpToCollectionCell:(baseBtn *)sender{
    [self.hyCollectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:sender.tag inSection:0] atScrollPosition:0 animated:NO];
}
#pragma mark - scrollview代理方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat offsetX = scrollView.contentOffset.x;
    CGFloat width = self.view.bounds.size.width;
    CGFloat scale = offsetX / width;
    NSInteger index = offsetX / width;
    CGFloat persent = scale - index;
    //下一个按钮的缩放比
    CGFloat nextPersent = persent;
    //当前按钮的缩放比
    CGFloat nowPersent = 1 - persent;
    //取出当前按钮
    baseBtn *nowBtn = self.btns[index];
    nowBtn.scale = nowPersent;
    NSInteger nextdex = index + 1;
    if (nextdex < self.topArray.count) {
        //        nextdex = index;
        baseBtn *nextBtn = self.btns[nextdex];
        nextBtn.scale = nextPersent;
        
    }
    
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    CGFloat offsetX = scrollView.contentOffset.x;
    CGFloat width = self.view.bounds.size.width;
    //取出当前的下标
    NSInteger index = offsetX / width;
    //取出对应的按钮
    baseBtn *btn = self.btns[index];
    [self didClickBtn:btn];
}

#pragma mark - 点击了穿越按钮
- (IBAction)didClickThroughBtn:(id)sender {
    
    BSXPFViewController *throughVc = [[BSXPFViewController alloc]init];
    [self.navigationController pushViewController:throughVc animated:YES];
}

#pragma mark - 按钮移动到中间
- (void)moveBtnToCenter:(baseBtn *)sender{
    //计算按钮的偏移值
    CGFloat btnOffsetX ;
    if(sender.frame.origin.x < self.view.bounds.size.width/2){
        btnOffsetX = 0;
    }else if (sender.frame.origin.x > (self.topArray.count - 4) * sender.bounds.size.width){
        btnOffsetX = self.hyScrollView.contentSize.width - self.view.bounds.size.width;
    }
    else{
        btnOffsetX = sender.center.x - self.view.bounds.size.width * 0.5;
        
    }
    [self.hyScrollView setContentOffset:CGPointMake(btnOffsetX, 0) animated:YES];
    
}

#pragma mark - 懒加载
- (NSMutableArray *)btns{
    if (!_btns) {
        _btns = [NSMutableArray array];
    }
    return _btns;
}
- (NSArray *)topArray{
    if (!_topArray) {
        _topArray = [HyScrollViewModel modelWithPlistName:@"hymain.plist"];
    }
    return _topArray;
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.topArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    HyScrollViewModel *model = self.topArray[indexPath.item];
    BSHyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"bsCell" forIndexPath:indexPath];
    cell.hyModel = model;
    return cell;
}
#pragma mark - viewDidAppear
- (void)viewDidAppear:(BOOL)animated{
    self.hyFlowLayout.itemSize = self.hyCollectionView.bounds.size;
}
#pragma mark - viewDidLoad
- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadDataAndAddBtns];
    self.hyCollectionView.delegate = self;
    self.hyCollectionView.dataSource = self;
    self.hyFlowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.hyFlowLayout.minimumInteritemSpacing = 0;
    self.hyFlowLayout.minimumLineSpacing = 0;
    self.hyCollectionView.pagingEnabled = YES;
    self.hyCollectionView.showsHorizontalScrollIndicator = NO;
}

@end
